using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace GenericExampleParticleSystem
{
    class ReadLidar
    {
        private string path;
        private char delimit;
        //private float[] x, y, z,i;
        private double[] dX, dY, dZ, dI;
        //private double rangeX, rangeY, rangeZ,maxX,maxY,maxZ,minX,minY,minZ;
        //private int NORTH =  - 381500; private int WEST = 4898000;
        //int lineNumbers;
        public ReadLidar(string _path, char _delimit)
        {
            path = _path;
            delimit = _delimit;
            ReadFile();
            //rangeX = Range(x);
            //rangeY = Range(y);
            //rangeZ = Range(z);
            //maxX = Max(x);
            //maxY = Max(y);
            //maxZ = Max(z);
            //minX = Min(x);
            //lineNumbers = lineNumbers();
        }

        private void ReadFile()
        {

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);

            string[] lineValues;
            int NumberOfLines = FileLines();

            dX = new double[NumberOfLines];
            dY = new double[NumberOfLines];
            dZ = new double[NumberOfLines];
            dI = new double[NumberOfLines];


            for (int j=0;j<NumberOfLines;j++)
            {
                lineValues = sr.ReadLine().Split(delimit);
                dX[j] = ((double)System.Convert.ToDouble(lineValues[0]));
                dY[j] = ((double)System.Convert.ToDouble(lineValues[1]));
                dZ[j] = (double)System.Convert.ToDouble(lineValues[2]);
                dI[j] = (double)System.Convert.ToDouble(lineValues[3]);
            }
            sr.Close();

            //MessageBox.Show("min:"+Min(dX).ToString()+"\nmax:"+Max(dX).ToString(),"read lidar manx min");
        }
        private int FileLines()
        {
            //Find the Number of lines in the file
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            int lines = 0;
            while (!sr.EndOfStream)
            {
                lines++;
                sr.ReadLine();
            }
            sr.Close();

            return lines;
        }

        public double Max(double[] data)
        {
            double high = (double)System.Double.NegativeInfinity;
            for (int j = 0;j< data.Length; j++)
            {
                if (data[j] > high)
                    high = data[j];
            }
            return high;
        }
        public double Min(double[] data)
        {
            double low = (double)System.Double.PositiveInfinity;
            for (int j = 0; j < data.Length; j++)
            {
                if (data[j] < low)
                    low = data[j];
            }
            return low;
        }
        public double Range(double[] data)
        {
            return Max(data) - Min(data);
        }
        private double[] getNormalized (double[] data)
        {
            double[] normalData = new double[data.Length];
            double dataRange = Range(data);
            double dataMax = Max(data);
            //for (int i=0;i<data.Length;i++)
            //{
            //    normalData[i] = ((dataRange - (dataMax - data[i]))/dataRange)*100;
            //}
            for (int i = 0; i < data.Length; i++)
            {
                normalData[i] = (data[i] - dataMax);
            }


            return normalData;
        }
        
        
        //GETTERS
        public double[] X
        { get { return dX; } }
        
        public double[] Y
        { get { return dY; } }
        
        public double[] Z
        { get { return dZ; } }
        public double[] I
        { get { return dI; } }
        public double[] NormalX
        { get { return getNormalized(dX); } }
        public double[] NormalY
        { get { return getNormalized(dY); } }
        public double[] NormalZ
        { get { return getNormalized(dZ); } }
        public double[] NormalI
        { get { return getNormalized(dI); } }
        public int elements
        { get { return FileLines(); } }
    }
}
